#ifndef LEAVES_H
#define LEAVES_H

#include "Mesh.h"
#include "FireObject.h"
#include <D3D11.h>
#include <d3dx11effect.h>
#include <xnamath.h>

struct LeafElement
{
	XMFLOAT4 pos;
	UINT type;
	float length;	
	float interval;
	UINT	id;
};

class Leaves : public Mesh, public FireObject
{
	public:
		Leaves();
		~Leaves();

		void init(const char* filename, ID3D11Device* g_device, ID3D11DeviceContext* g_context);
		void draw(const Camera& camera, const vector<Light*>& lights);
		void draw(int subset, ID3DX11Effect* fx, const char* technique) const;
		void Update(float dt);
		
		void setActive(bool v);
		void setFirstRun(bool v) { bFirstRun = v; }

		bool animationFinished() { return m_bFinishedAnimation; }

		void generateEmitters(UINT subset, UINT global_ID, ID3DX11Effect* fxDefault, const char* techDefault);
		XMFLOAT4X4 getSubsetMatrix(UINT subset) const;

		void drop() { m_bLeavesDrop = true; }

		XMMATRIX getWorldMatrix(int subset) const;

	private:
		void computeLeafMatrix(XMFLOAT3 vPos, UINT leaf_index);

		bool bActive;
		bool  bFirstRun;

		ID3D11ShaderResourceView* srv_noise_texture;
		ID3D11Buffer*	vb_init;
		ID3D11Buffer*	vb_elements;
		ID3D11Buffer*	vb_elements_2;

		ID3D11Buffer*	vb_ID;

		ID3D11InputLayout* IA_Leaf;
		ID3D11InputLayout* IA_Draw;

		bool	m_bFinishedAnimation;
		bool	m_bLeavesDrop;
		
		static const unsigned int MAX_ELEMENTS = 20000;
		
		

		static const unsigned int LEAF_GENERATOR = 0;
		static const unsigned int LEAF_MAIN = 1;
		static const unsigned int LEAF_SIDE = 2;
		static const unsigned int N_LEAVES = 32;

		////////////
		////////////

		XMFLOAT4X4	mtxLeaves[N_LEAVES];

		
		ID3DX11EffectMatrixVariable* fx_m_World;
		ID3DX11EffectMatrixVariable* fx_m_WorldViewProj;
		ID3DX11EffectMatrixVariable* fx_m_L_ViewProj;

		ID3DX11EffectShaderResourceVariable* fx_tex_shadow_map;

		ID3DX11EffectVariable*	fx_lights;

		ID3DX11EffectScalarVariable*	fx_num_lights;

		ID3DX11EffectVectorVariable*	fx_pEye;
		ID3DX11EffectScalarVariable*	fx_bShadowed;
		ID3DX11EffectScalarVariable*	fx_bUsePCSS;
};



#endif